import { defineStore } from 'pinia'
import { ref } from 'vue'

const useCommentStore = defineStore(
  'comment',
  () => {
    // state、getter、action
    const comments = ref([
      // { id: 1, content: '非常精彩' },
      // { id: 2, content: '好搞笑' },
      // { id: 3, content: '有同学在摸鱼' },
    ])
    const count = ref(1)
    const price = ref(10)
    const incCount = () => {
      count.value++
    }
    const incPrice = () => {
      price.value++
    }

    // 发布评论
    const pub = (content) => {
      comments.value.push({ id: Date.now(), content: content })
    }

    // 删除评论
    const del = (id) => {
      let index = comments.value.findIndex((item) => item.id === id)
      comments.value.splice(index, 1)
    }

    return {
      comments,
      count,
      price,
      pub,
      del,
      incCount,
      incPrice,
    }
  },
  {
    // 将此仓库中的所有的数据持久化到本地存储
    persist: {
      key: 'mycomment',
      pick: ['comments', 'count'],
    },
  }
)

export default useCommentStore
